package com.ch.base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import com.ch.base.obj.DateObj;

public class DateUtils {
	/**
	 * <b>民國日期轉西元日期</b>
	 * @param 	origDate	民國日期
	 * @return 	西元日期
	 */
	public static String cDateToWDate(String origDate){
		String result = "";
		if (StringUtils.isNotEmpty(origDate)) {
			String date[] = origDate.split("/");
			String dateYear = date[0];
			if (dateYear.subSequence(0, 1).equals("0")) {
				dateYear = dateYear.substring(1, 3);
			}
			dateYear = String.valueOf(Integer.parseInt(dateYear) + 1911);
			result = dateYear + "/" + date[1] + "/" + date[2];
		}
		return result;
	}

	/**
	 * <b>西元日期轉民國日期</b>
	 * @param 	origDate	西元日期
	 * @return 	民國日期
	 */
	public static String wDateToCDate(String origDate){
		String result = "";
		if (StringUtils.isNotEmpty(origDate)) {
			String date[] = origDate.split("/");
			String dateYear = date[0];
			dateYear = String.valueOf(Integer.parseInt(dateYear) - 1911);
			dateYear = StringUtils.lPad(dateYear, 3, "0");
			result = dateYear + "/" + date[1] + "/" + date[2];
		}
		return result;
	}

	/**
	 * <b>取得目前的日期時間</b>
	 * @param 	dateType	日期型態可分為AD(西元年)以及ROC(民國年)，預設為AD
	 * @return 	DateObject物件
	 * 			dateObject.getYear()		年
	 * 			dateObject.getMonth() 		月
	 * 			dateObject.getDay() 		日
	 * 			dateObject.getHour() 		小時
	 * 			dateObject.getMinute() 		分
	 * 			dateObject.getSecond() 		秒
	 * 			dateObject.getMillisecond() 毫秒
	 */
	public static DateObj getNowDateTime(String dateType){
		Calendar rightNowDateTime = Calendar.getInstance();
		String year = String.valueOf(rightNowDateTime.get(Calendar.YEAR));
		String month = String.valueOf(rightNowDateTime.get(Calendar.MONTH) + 1);
		String day = String.valueOf(rightNowDateTime.get(Calendar.DATE));
		//Calendar.HOUR_OF_DAY 24hr, Calendar.HOUR 12hr
		String hour = String.valueOf(rightNowDateTime.get(Calendar.HOUR_OF_DAY));
		String minute = String.valueOf(rightNowDateTime.get(Calendar.MINUTE));
		String second = String.valueOf(rightNowDateTime.get(Calendar.SECOND));
		String millisecond = String.valueOf(rightNowDateTime.get(Calendar.MILLISECOND));

		if (dateType.equals("ROC")) {
			year = String.valueOf(Integer.parseInt(year) - 1911);
			year = StringUtils.lPad(year, 3, "0");
		}

		month = StringUtils.lPad(month, 2, "0");
		day = StringUtils.lPad(day, 2, "0");
		hour = StringUtils.lPad(hour, 2, "0");
		minute = StringUtils.lPad(minute, 2, "0");
		second = StringUtils.lPad(second, 2, "0");
		millisecond = StringUtils.lPad(millisecond, 3, "0");

		DateObj dateObj = new DateObj();
		dateObj.setYear(year);
		dateObj.setMonth(month);
		dateObj.setDay(day);
		dateObj.setHour(hour);
		dateObj.setMinute(minute);
		dateObj.setSecond(second);
		dateObj.setMillisecond(millisecond);
		return dateObj;
	}

	/**
	 * <b>取得系統時間</b><br/>
	 * 如：DateUtil.getSysTime("ROC", "", true, "", true, true) -> 100/06/22 00:11:00.719
	 * @param	dateType 		日期型態可分為AD(西元年)以及ROC(民國年)，預設為AD
	 * @param 	dateSplit 		日期分隔符號
	 * @param 	showTime 		顯示時間與否
	 * @param 	connectionSplit 日期時間分隔符號
	 * @param 	showTimeSplit 	顯示時間分隔符號與否
	 * @param 	showMillisecond 顯示毫秒與否
	 * @return 	目前的日期時間
	 */
	public static String getSysTime(String dateType, String dateSplit, boolean showTime, String connectionSplit, boolean showTimeSplit, boolean showMillisecond){
		Calendar rightNowDateTime = Calendar.getInstance();
		String year = String.valueOf(rightNowDateTime.get(Calendar.YEAR));
		String month = String.valueOf(rightNowDateTime.get(Calendar.MONTH)+1);
		String day = String.valueOf(rightNowDateTime.get(Calendar.DATE));
		//Calendar.HOUR_OF_DAY 24hr, Calendar.HOUR 12hr
		String hour = String.valueOf(rightNowDateTime.get(Calendar.HOUR_OF_DAY));
		String minute = String.valueOf(rightNowDateTime.get(Calendar.MINUTE));
		String second = String.valueOf(rightNowDateTime.get(Calendar.SECOND));
		String millisecond = String.valueOf(rightNowDateTime.get(Calendar.MILLISECOND));

		month = StringUtils.lPad(month, 2, "0");
		day = StringUtils.lPad(day, 2, "0");
		hour = StringUtils.lPad(hour, 2, "0");
		minute = StringUtils.lPad(minute, 2, "0");
		second = StringUtils.lPad(second, 2, "0");
		millisecond = StringUtils.lPad(millisecond, 3, "0");

		String result = "";
		String date = year + "/" + month + "/" + day;
		if (dateType.equals("ROC")) {
			date = wDateToCDate(date);
		}

		if (dateSplit.equals("none")) {
			date = date.replace("/", "");
		} else if (!((dateSplit.equals("none")) || (dateSplit.equals("")))) {
			date = date.replace("/", dateSplit);
		}

		if (showTime == true) {
			String time = "";
			if (showTimeSplit == true) {
				time = hour + ":" + minute + ":" + second;
				if (showMillisecond == true) {
					time = time + "." + millisecond;
				}
			} else {
				time = hour + minute + second;
				if (showMillisecond == true) {
					time = time + millisecond;
				}
			}

			if (connectionSplit.equals("none")) {
				result = date + time;
			} else if (!((connectionSplit.equals("none")) || (connectionSplit.equals("")))){
				result = date + connectionSplit + time;
			} else {
				result = date + " " + time;
			}
		}else{
			result = date;
		}
		return result;
	}

	/**
	 * <b>民國日期加減</b>
	 * @param	origDate	民國日期
	 * @param	calcType	欲加減之日期型別：day(預設), week, month, year
	 * @param	num			欲增減數目
	 * @return	計算完畢之日期
	 */
	public static String calcCDate(String origDate, String calcType, int num) {
		String result = "";
		if (StringUtils.isNotEmpty(origDate)) {
			result = wDateToCDate(calcWDate(cDateToWDate(origDate), calcType, num));
		}
		return result;
	}

	/**
	 * <b>西元日期加減</b>
	 * @param	origDate	西元日期
	 * @param	calcType	欲加減之日期型別：day(預設), week, month, year
	 * @param	num			欲增減數目
	 * @return	計算完畢之日期
	 */
	public static String calcWDate(String origDate, String calcType, int num) {
		String result = "";
		if (StringUtils.isNotEmpty(origDate)) {
			String[] dateArray = origDate.split("/");
			SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy/MM/dd");
			Calendar calendar = Calendar.getInstance();
			int year = Integer.parseInt(dateArray[0]);
			int month = Integer.parseInt(dateArray[1]) - 1;
			int day = Integer.parseInt(dateArray[2]);
			calendar.set(calendar.YEAR, year);
			calendar.set(calendar.MONTH, month);
			calendar.set(calendar.DATE, day);

			if ((StringUtils.isEmpty(calcType)) || (calcType.equals("day"))) {
				calendar.add(Calendar.DATE, num);
			} else if (calcType.equals("week")) {
				calendar.add(Calendar.WEEK_OF_MONTH, num);
			} else if (calcType.equals("month")) {
				calendar.add(Calendar.MONTH, num);
			} else if (calcType.equals("year")) {
				calendar.add(Calendar.YEAR, num);
			}

			result = sdf.format(calendar.getTime());
		}
		return result;
	}


	/**
	 * <b>取得民國某年某月的天數</b>
	 * @param	year	民國年
	 * @param	month 	月份
	 * @return 	天數
	 */
	public static String cDaysOfMonth(String year, String month){
		String result = "";
		if (StringUtils.isNotEmpty(year) && StringUtils.isNotEmpty(month)) {
			result = wDaysOfMonth(String.valueOf(Integer.parseInt(year)+1911), month);
		}
		return result;
	}

	/**
	 * <b>取得西元某年某月的天數</b>
	 * @param	year	西元年
	 * @param	month 	月份
	 * @return 	天數
	 */
	public static String wDaysOfMonth(String year, String month){
		String result = "";
		if (StringUtils.isNotEmpty(year) && StringUtils.isNotEmpty(month)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
			if(Integer.parseInt(year) < 1900){
				year = "1900";
			}
			if(Integer.parseInt(month) < 1){
				month = "1";
			}
			if(Integer.parseInt(month) > 12){
				month = "12";
			}

			String temp = year + "/" + (((Integer.parseInt(month)+1) < 10) ? "0" : "") + (Integer.parseInt(month)+1) + "/01";
			try {
				Date date = sdf.parse(temp);
				date.setTime(date.getTime() - 86400*1000);
				Calendar calendar = new GregorianCalendar();
				calendar.setTime(date);
				result = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 計算相差時間
	 * @param	differenceType	欲計算之單位：day, hour, minute, second(預設)
	 * @param	startDatetime
	 * @param	endDatetime
	 * @return
	 */
	public static long calcDifferenceDateTime(String differenceType, long startDatetime, long endDatetime) {
		long result = 0;
		long difference = endDatetime - startDatetime;
				
		differenceType = StringUtils.isNotEmpty(differenceType) ? differenceType : "second";
		
		if (differenceType.equals("day")) {
			//相差天數
			result= difference / (3600 * 24 * 1000);
		} else if (differenceType.equals("hour")) {
			//相差小時：
			result = difference / (3600 * 1000);
		} else if (differenceType.equals("minute")) {
			//相差分鐘：
			result = difference/(60 * 1000);
		} else if (differenceType.equals("second")) {
			//相差秒： 
			result = difference / 1000;
		}
		
		return result;
	}

}
